package utils

import (
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

type SqlFile struct {
	Path   string //文件的绝对路径
	maxClo int    //最大行号
	idx    int    //读取到当前文件第几行
	head   string
	col    []string //行
}

func NewSqlFile(path string) (*SqlFile, error) {
	sqlFile := &SqlFile{Path: path, idx: 1}
	fileHanle, err := os.OpenFile(path, os.O_RDONLY, 0666)
	if err != nil {
		return nil, err
	}

	defer fileHanle.Close()

	readBytes, err := ioutil.ReadAll(fileHanle)
	if err != nil {
		return nil, err
	}

	results := strings.Split(string(readBytes), "\n")

	sqlFile.col = results
	sqlFile.head = strings.TrimSpace(results[0])
	sqlFile.maxClo = len(results)
	return sqlFile, nil
}

func (s *SqlFile) HasNext() bool {
	if s.maxClo > s.idx {
		return true
	} else {
		return false
	}
}

func (s *SqlFile) Next() string {
	// 定义Buffer类型
	v := strings.Replace(s.col[s.idx], "),", ");", 1)
	res := fmt.Sprintf("%s%s", s.head, v)
	s.idx++
	return res
}
